home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / DJGPP / BCCGRX12.ZIP / contrib / bcc2grx / test / bccbgi.c next >
C/C++ Source or Header  |  1993-07-24  |  51KB  |  1,461 lines

  1. /*
  2.    GRAPHICS DEMO FOR Borland C++
  3.  
  4.    Copyright (c) 1987,88,91 Borland International. All rights reserved.
  5.  
  6.    From the command line, use:
  7.  
  8.     bcc bgidemo graphics.lib
  9.  
  10. */
  11. /* Partially copyrighted (c) 1993 by Hartmut Schirmer */
  12.  
  13. #ifdef __TINY__
  14. #error BGIDEMO will not run in the tiny model.
  15. #endif
  16.  
  17. #include <dos.h>
  18. #include <math.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <stdarg.h>
  22.  
  23. #ifdef __GNUC__
  24. #  define BGI_PATH "..\\chr"
  25. #  include <pc.h>
  26. #  include "libbcc.h"
  27. #  define random(a) RANDOM_TP(a)
  28. #  define srand(s)  (RandSeed = (s))
  29. #  define itoa(value,str,radix) sprintf((str),"%d",(value))
  30. #  define getch() getkey()
  31. #else
  32. #  define BGI_PATH "c:\\bc\\bgi"
  33. #  include <conio.h>
  34. #  include <graphics.h>
  35. #endif
  36.  
  37. #include "../include/bgiext.h"
  38.  
  39. #define ESC     0x1b                    /* Define the escape key        */
  40. #ifndef TRUE
  41. #  define TRUE  1                       /* Define some handy constants  */
  42. #endif
  43. #ifndef FALSE
  44. #  define FALSE 0                       /* Define some handy constants  */
  45. #endif
  46. #ifndef PI
  47. #  define PI    3.14159                 /* Define a value for PI        */
  48. #endif
  49. #define ON      1                       /* Define some handy constants  */
  50. #define OFF     0                       /* Define some handy constants  */
  51.  
  52. #define NFONTS 11
  53.  
  54. char *Fonts[] = {
  55.   "DefaultFont",   "TriplexFont",   "SmallFont",
  56.   "SansSerifFont", "GothicFont", "ScriptFont", "SimplexFont", "TriplexScriptFont",
  57.   "ComplexFont", "EuropeanFont", "BoldFont"
  58. };
  59.  
  60. char *LineStyles[] = {
  61.   "SolidLn",  "DottedLn",  "CenterLn",  "DashedLn",  "UserBitLn"
  62. };
  63.  
  64. char *FillStyles[] = {
  65.   "EmptyFill",  "SolidFill",      "LineFill",      "LtSlashFill",
  66.   "SlashFill",  "BkSlashFill",    "LtBkSlashFill", "HatchFill",
  67.   "XHatchFill", "InterleaveFill", "WideDotFill",   "CloseDotFill"
  68. };
  69.  
  70. char *TextDirect[] = {
  71.   "HorizDir",  "VertDir"
  72. };
  73.  
  74. char *HorizJust[] = {
  75.   "LeftText",   "CenterText",   "RightText"
  76. };
  77.  
  78. char *VertJust[] = {
  79.   "BottomText",  "CenterText",  "TopText"
  80. };
  81.  
  82. struct PTS {
  83.   int x, y;
  84. };      /* Structure to hold vertex points      */
  85.  
  86. int    GraphDriver;             /* The Graphics device driver           */
  87. int    GraphMode;               /* The Graphics mode value              */
  88. double AspectRatio;             /* Aspect ratio of a pixel on the screen*/
  89. int    MaxX, MaxY;              /* The maximum resolution of the screen */
  90. int    MaxColors;               /* The maximum # of colors available    */
  91. int    ErrorCode;               /* Reports any graphics errors          */
  92. struct palettetype palette;             /* Used to read palette info    */
  93. static char PauseMsg[] = "Esc aborts or press a key...";
  94. static char StopMsg[]  = "ESC Aborts - Press a Key to stop";
  95.  
  96. /*                                                                      */
  97. /*      GPRINTF: Used like PRINTF except the output is sent to the      */
  98. /*      screen in graphics mode at the specified co-ordinate.           */
  99. /*                                                                      */
  100.  
  101. int gprintf( int *xloc, int *yloc, char *fmt, ... )
  102. {
  103.   va_list  argptr;                      /* Argument list pointer        */
  104.   char str[140];                        /* Buffer to build sting into   */
  105.   int cnt;                              /* Result of SPRINTF for return */
  106.  
  107.   va_start( argptr, fmt );              /* Initialize va_ functions     */
  108.  
  109.   cnt = vsprintf( str, fmt, argptr );   /* prints string to buffer      */
  110.   outtextxy( *xloc, *yloc, str );       /* Send string in graphics mode */
  111.   *yloc += textheight( "H" ) + 2;       /* Advance to next line         */
  112.  
  113.   va_end( argptr );                     /* Close va_ functions          */
  114.  
  115.   return( cnt );                        /* Return the conversion count  */
  116.  
  117. }
  118.  
  119. /*                                                                      */
  120. /*      CHANGETEXTSTYLE: similar to settextstyle, but checks for        */
  121. /*      errors that might occur whil loading the font file.             */
  122. /*                                                                      */
  123.  
  124. void changetextstyle(int font, int direction, int charsize)
  125. {
  126.   int ErrorCode;
  127.  
  128.   graphresult();                        /* clear error code             */
  129.   settextstyle(font, direction, charsize);
  130.   ErrorCode = graphresult();            /* check result                 */
  131.   if( ErrorCode != grOk ){              /* if error occured             */
  132.     closegraph();
  133.     printf(" Graphics System Error: %s\n", grapherrormsg( ErrorCode ) );
  134.     exit( 1 );
  135.   }
  136. }
  137.  
  138. /*                                                                      */
  139. /*      DRAWBORDER: Draw a solid single line around the current         */
  140. /*      viewport.                                                       */
  141. /*                                                                      */
  142.  
  143. void DrawBorder(int color)
  144. {
  145.   struct viewporttype vp;
  146.  
  147.   setcolor( color);
  148.  
  149.   setlinestyle( SOLID_LINE, 0, NORM_WIDTH );
  150.  
  151.   getviewsettings( &vp );
  152.   rectangle( 0, 0, vp.right-vp.left, vp.bottom-vp.top);
  153.  
  154. }
  155.  
  156. /*                                                                      */
  157. /*      STATUSLINE: Display a status line at the bottom of the screen.  */
  158. /*                                                                      */
  159.  
  160. void StatusLineColor( char *msg, int color )
  161. {
  162.   int height;
  163.  
  164.   setviewport( 0, 0, MaxX, MaxY, 1 );   /* Open port to full screen     */
  165.   setcolor( color);                     /* Set requested color          */
  166.  
  167.   changetextstyle( DEFAULT_FONT, HORIZ_DIR, 1 );
  168.   settextjustify( CENTER_TEXT, TOP_TEXT );
  169.   setlinestyle( SOLID_LINE, 0, NORM_WIDTH );
  170.   setfillstyle( EMPTY_FILL, 0 );
  171.  
  172.   height = textheight( "H" );           /* Detemine current height      */
  173.   bar( 0, MaxY-(height+4), MaxX, MaxY );
  174.   rectangle( 0, MaxY-(height+4), MaxX, MaxY );
  175.   outtextxy( MaxX/2, MaxY-(height+2), msg );
  176.   setviewport( 1, height+5, MaxX-1, MaxY-(height+5), 1 );
  177. }
  178.  
  179. void StatusLine( char *msg )
  180. {
  181.   StatusLineColor(msg, WHITE);
  182. }
  183.  
  184. /*                                                                      */
  185. /*      MAINWINDOW: Establish the main window for the demo and set      */
  186. /*      a viewport for the demo code.                                   */
  187. /*                                                                      */
  188.  
  189. void MainWindowColor( char *header, int color)
  190. {
  191.   int height;
  192.  
  193.   cleardevice();                        /* Clear graphics screen        */
  194.   setcolor( color);                     /* Set current requested color  */
  195.   setviewport( 0, 0, MaxX, MaxY, 1 );   /* Open port to full screen     */
  196.  
  197.   height = textheight( "H" );           /* Get basic text height        */
  198.  
  199.   changetextstyle( DEFAULT_FONT, HORIZ_DIR, 1 );
  200.   settextjustify( CENTER_TEXT, TOP_TEXT );
  201.   outtextxy( MaxX/2, 2, header );
  202.   setviewport( 0, height+4, MaxX, MaxY-(height+4), 1 );
  203.   DrawBorder(color);
  204.   setviewport( 1, height+5, MaxX-1, MaxY-(height+5), 1 );
  205.  
  206. }
  207.  
  208. void MainWindow( char *header )
  209. {
  210.   MainWindowColor( header, WHITE);
  211. }
  212.  
  213. /*                                                                      */
  214. /*      PAUSE: Pause until the user enters a keystroke. If the          */
  215. /*      key is an ESC, then exit program, else simply return.           */
  216. /*                                                                      */
  217.  
  218.  
  219. void Pause(void)
  220. {
  221.   int c;
  222.  
  223.   StatusLine( PauseMsg );               /* Put msg at bottom of screen  */
  224.  
  225.   c = getch();                          /* Read a character from kbd    */
  226.  
  227.   if( ESC == c ){                       /* Does user wish to leave?     */
  228.     closegraph();                       /* Change to text mode          */
  229.     exit( 1 );                          /* Return to OS                 */
  230.   }
  231.  
  232.   if( 0 == c ){                         /* Did use hit a non-ASCII key? */
  233.     c = getch();                        /* Read scan code for keyboard  */
  234.   }
  235.  
  236.   cleardevice();                        /* Clear the screen             */
  237.  
  238. }
  239.  
  240. /*                                                                      */
  241. /*      INITIALIZE: Initializes the graphics system and reports         */
  242. /*      any errors which occured.                                       */
  243. /*                                                                      */
  244.  
  245. void Initialize(void)
  246. {
  247.   int xasp, yasp;                       /* Used to read the aspect ratio*/
  248.  
  249.   GraphDriver = DETECT;                 /* Request auto-detection       */
  250.   initgraph( &GraphDriver, &GraphMode, BGI_PATH );
  251.   ErrorCode = graphresult();            /* Read result of initialization*/
  252.   if( ErrorCode != grOk ){              /* Error occured during init    */
  253.     printf(" Graphics System Error: %s\n", grapherrormsg( ErrorCode ) );
  254.     exit( 1 );
  255.   }
  256.  
  257.   getpalette( &palette );               /* Read the palette from board  */
  258.   MaxColors = getmaxcolor() + 1;        /* Read maximum number of colors*/
  259.   if (MaxColors > 16)
  260.     setrgbdefaults();
  261.  
  262.   MaxX = getmaxx();
  263.   MaxY = getmaxy();                     /* Read size of screen          */
  264.  
  265.   getaspectratio( &xasp, &yasp );       /* read the hardware aspect     */
  266.   AspectRatio = (double)xasp / (double)yasp; /* Get correction factor   */
  267.  
  268. }
  269.  
  270.  
  271. /*                                                                      */
  272. /*      REPORTSTATUS: Report the current configuration of the system    */
  273. /*      after the auto-detect initialization.                           */
  274. /*                                                                      */
  275.  
  276. void ReportStatus(void)
  277. {
  278.   struct viewporttype     viewinfo;     /* Params for inquiry procedures*/
  279.   struct linesettingstype lineinfo;
  280.   struct fillsettingstype fillinfo;
  281.   struct textsettingstype textinfo;
  282.   struct palettetype      palette;
  283.  
  284.   char *driver, *mode, *fmt;            /* Strings for driver and mode  */
  285.   int x, y, mno;
  286.  
  287.   getviewsettings( &viewinfo );
  288.   getlinesettings( &lineinfo );
  289.   getfillsettings( &fillinfo );
  290.   gettextsettings( &textinfo );
  291.   getpalette( &palette );
  292.  
  293.   x = 5;
  294.   y = 4;
  295.  
  296.   MainWindow( "Status report after InitGraph" );
  297.   settextjustify( LEFT_TEXT, TOP_TEXT );
  298.  
  299.   driver = getdrivername();
  300.   mode = getmodename(GraphMode);        /* get current setting          */
  301.  
  302.   gprintf( &x, &y, "Graphics device    : %-20s (%d)", driver, GraphDriver );
  303.   gprintf( &x, &y, "Graphics mode      : %-20s (%d)", mode, GraphMode );
  304.   gprintf( &x, &y, "Screen resolution  : ( 0, 0, %d, %d )", getmaxx(), getmaxy() );
  305.  
  306.   gprintf( &x, &y, "Current view port  : ( %d, %d, %d, %d )",
  307.   viewinfo.left, viewinfo.top, viewinfo.right, viewinfo.bottom );
  308.   gprintf( &x, &y, "Clipping           : %s", viewinfo.clip ? "ON" : "OFF" );
  309.  
  310.   gprintf( &x, &y, "Current position   : ( %d, %d )", getx(), gety() );
  311.   gprintf( &x, &y, "Colors available   : %d", MaxColors );
  312.   gprintf( &x, &y, "Current color      : %d", getcolor() );
  313.  
  314.   gprintf( &x, &y, "Line style         : %s", LineStyles[ lineinfo.linestyle ] );
  315.   gprintf( &x, &y, "Line thickness     : %d", lineinfo.thickness );
  316.  
  317.   gprintf( &x, &y, "Current fill style : %s", FillStyles[ fillinfo.pattern ] );
  318.   gprintf( &x, &y, "Current fill color : %d", fillinfo.color );
  319.  
  320.   gprintf( &x, &y, "Current font       : %s", Fonts[ textinfo.font ] );
  321.   gprintf( &x, &y, "Text direction     : %s", TextDirect[ textinfo.direction ] );
  322.   gprintf( &x, &y, "Character size     : %d", textinfo.charsize );
  323.   gprintf( &x, &y, "Horizontal justify : %s", HorizJust[ textinfo.horiz ] );
  324.   gprintf( &x, &y, "Vertical justify   : %s", VertJust[ textinfo.vert ] );
  325.  
  326.   if (MaxY<350-1) {
  327.     x = 400;
  328.     y = 4;
  329.     fmt = "#%-3d: %s";
  330.   } else {
  331.     y += 5;
  332.     fmt = "Mode #%-3d          : %s";
  333.   }
  334.   gprintf( &x, &y, "Available modes :");
  335.   y += 5;
  336.   for (mno = 0; mno <= getmaxmode(); ++mno)
  337.       gprintf( &x, &y, fmt, mno, getmodename(mno));
  338.  
  339.   Pause();                              /* Pause for user to read screen*/
  340.  
  341. }
  342.  
  343. /*                                                                      */
  344. /*      TEXTDUMP: Display the all the characters in each of the         */
  345. /*      available fonts.                                                */
  346. /*                                                                      */
  347.  
  348. void TextDump(void)
  349. {
  350.   static int CGASizes[]  = {
  351.     1, 3, 7, 3, 3, 2, 2, 2, 2, 2, 2  };
  352.   static int NormSizes[] = {
  353.     1, 4, 7, 4, 4, 2, 2, 2, 2, 2, 2  };
  354.  
  355.   char buffer[80];
  356.   int font, ch, wwidth, lwidth, size;
  357.   struct viewporttype vp;
  358.  
  359.   for( font=0 ; font<NFONTS ; ++font ){ /* For each available font      */
  360.     sprintf( buffer, "%s Character Set", Fonts[font] );
  361.     MainWindow( buffer );               /* Display fontname as banner   */
  362.     getviewsettings( &vp );             /* read current viewport        */
  363.  
  364.     settextjustify( LEFT_TEXT, TOP_TEXT );
  365.     moveto( 5, 3 );
  366.  
  367.     buffer[1] = '\0';                   /* Terminate string             */
  368.     wwidth = vp.right - vp.left;        /* Determine the window width   */
  369.  
  370.     if( font == DEFAULT_FONT ){
  371.       changetextstyle( font, HORIZ_DIR, 1 );
  372.       lwidth = textwidth( "H" );        /* Get average letter width     */
  373.       ch = 0;
  374.       while( ch < 256 ){                /* For each possible character  */
  375.     buffer[0] = ch;                 /* Put character into a string  */
  376.     if( (getx() + lwidth) > wwidth-3)
  377.       moveto( 5, gety() + textheight("H") + 3 );
  378.     outtext( buffer );              /* send string to screen        */
  379.     ++ch;                           /* Goto the next character      */
  380.       }
  381.     }
  382.     else{
  383.  
  384.       size = (MaxY < 200) ? CGASizes[font] : NormSizes[font];
  385.       changetextstyle( font, HORIZ_DIR, size );
  386.  
  387.       ch = '!';                         /* Begin at 1st printable       */
  388.       while( ch < 256 ){                /* For each printable character */
  389.     buffer[0] = ch;                 /* Put character into a string  */
  390.     lwidth = textwidth( buffer);    /* Get letter width             */
  391.     if( (lwidth+getx()) > wwidth-3) /* Are we still in window?      */
  392.       moveto( 5, gety()+textheight("H")+3 );
  393.     outtext( buffer );              /* send string to screen        */
  394.     ++ch;                           /* Goto the next character      */
  395.       }
  396.  
  397.     }
  398.  
  399.     Pause();                            /* Pause until user acks        */
  400.  
  401.   }                                     /* End of FONT loop             */
  402.  
  403. }
  404.  
  405. /*                                                                      */
  406. /*      BAR3DDEMO: Display a 3-D bar chart on the screen.               */
  407. /*                                                                      */
  408.  
  409. void Bar3DDemo(void)
  410. {
  411.   static int barheight[] = {
  412.     1, 3, 5, 4, 3, 2, 1, 5, 4, 2, 3   };
  413.   struct viewporttype vp;
  414.   int xstep, ystep, deepth;
  415.   int i, j, h, color, bheight;
  416.   char buffer[10];
  417.  
  418.   MainWindow( "Bar 3-D / Rectangle Demonstration" );
  419.  
  420.   h = 3 * textheight( "H" );
  421.   getviewsettings( &vp );
  422.   settextjustify( CENTER_TEXT, TOP_TEXT );
  423.   changetextstyle( TRIPLEX_FONT, HORIZ_DIR, 4 );
  424.   outtextxy( MaxX/2, 6, "These are 3-D Bars" );
  425.   changetextstyle( DEFAULT_FONT, HORIZ_DIR, 1 );
  426.   setviewport( vp.left+50, vp.top+40, vp.right-50, vp.bottom-10, 1 );
  427.   getviewsettings( &vp );
  428.  
  429.   line( h, h, h, vp.bottom-vp.top-h );
  430.   line( h, (vp.bottom-vp.top)-h, (vp.right-vp.left)-h, (vp.bottom-vp.top)-h );
  431.   xstep = ((vp.right-vp.left) - (2*h)) / 10;
  432.   ystep = ((vp.bottom-vp.top) - (2*h)) / 5;
  433.   j = (vp.bottom-vp.top) - h;
  434.   deepth = (getmaxx() <= 400) ? 7 : 15;
  435.   settextjustify( LEFT_TEXT, CENTER_TEXT );
  436.  
  437.   for( i=0 ; i<6 ; ++i ){
  438.     line( h/2, j, h, j );
  439.     itoa( i, buffer, 10 );
  440.     outtextxy( 0, j, buffer );
  441.     j -= ystep;
  442.   }
  443.  
  444.   j = h;
  445.   settextjustify( CENTER_TEXT, TOP_TEXT );
  446.  
  447.   for( i=0 ; i<11 ; ++i ){
  448.     color = random( MaxColors );
  449.     setfillstyle( i+1, color );
  450.     line( j, (vp.bottom-vp.top)-h, j, (vp.bottom-vp.top-3)-(h/2) );
  451.     itoa( i, buffer, 10 );
  452.     outtextxy( j, (vp.bottom-vp.top)-(h/2), buffer );
  453.     if( i != 10 ){
  454.       bheight = (vp.bottom-vp.top) - h - 1;
  455.       bar3d( j, (vp.bottom-vp.top-h)-(barheight[i]*ystep), j+xstep-deepth, bheight, deepth, 1 );
  456.     }
  457.     j += xstep;
  458.   }
  459.  
  460.   Pause();                              /* Pause for user's response    */
  461.  
  462. }
  463.  
  464. /*                                                                      */
  465. /*      RANDOMBARS: Display random bars                                 */
  466. /*                                                                      */
  467.  
  468. void RandomBars(void)
  469. {
  470.   int color;
  471.  
  472.   MainWindow( "Random Bars" );
  473.   StatusLine( PauseMsg );               /* Put msg at bottom of screen   */
  474.   while( !kbhit() ){                    /* Until user enters a key...   */
  475.     color = random( MaxColors-1 )+1;
  476.     setcolor( color );
  477.     setfillstyle( random(11)+1, color );
  478.     bar3d( random( getmaxx() ), random( getmaxy() ),
  479.        random( getmaxx() ), random( getmaxy() ), 0, OFF);
  480.   }
  481.  
  482.   Pause();                              /* Pause for user's response    */
  483.  
  484. }
  485.  
  486.  
  487. /*                                                                      */
  488. /*      TEXTDEMO: Show each font in several sizes to the user.          */
  489. /*                                                                      */
  490.  
  491. void TextDemo(void)
  492. {
  493.   int charsize[] = {
  494.     1, 3, 7, 3, 4, 2, 2, 2, 2, 2, 2   };
  495.   int font, size;
  496.   int h, x, y, i;
  497.   struct viewporttype vp;
  498.   char buffer[80];
  499.  
  500.   for( font=0 ; font<NFONTS ; ++font ){ /* For each of the avail. fonts */
  501.  
  502.     sprintf( buffer, "%s Demonstration", Fonts[font] );
  503.     MainWindow( buffer );
  504.     getviewsettings( &vp );
  505.  
  506.     changetextstyle( font, VERT_DIR, charsize[font] );
  507.     settextjustify( CENTER_TEXT, BOTTOM_TEXT );
  508.     outtextxy( 2*textwidth("M"), vp.bottom - 2*textheight("M"), "Vertical" );
  509.  
  510.     changetextstyle( font, HORIZ_DIR, charsize[font] );
  511.     settextjustify( LEFT_TEXT, TOP_TEXT );
  512.     outtextxy( 2*textwidth("M"), 2, "Horizontal" );
  513.  
  514.     settextjustify( CENTER_TEXT, CENTER_TEXT );
  515.     x = (vp.right - vp.left) / 2;
  516.     y = textheight( "H" );
  517.  
  518.     for( i=1 ; i<5 ; ++i ){             /* For each of the sizes */
  519.       size = (font == SMALL_FONT) ? i+3 : i;
  520.       changetextstyle( font, HORIZ_DIR, size );
  521.       h = textheight( "H" );
  522.       y += h;
  523.       sprintf( buffer, "Size %d", size );
  524.       outtextxy( x, y, buffer );
  525.  
  526.     }
  527.  
  528.     if( font != DEFAULT_FONT ){         /* Show user declared font size */
  529.       y += h / 2;                       /* Move down the screen         */
  530.       settextjustify( CENTER_TEXT, TOP_TEXT );
  531.       setusercharsize( 5, 6, 3, 2 );
  532.       changetextstyle( font, HORIZ_DIR, USER_CHAR_SIZE );
  533.       outtextxy( (vp.right-vp.left)/2, y, "User Defined Size" );
  534.     }
  535.  
  536.     Pause();                            /* Pause to let user look       */
  537.  
  538.   }                                     /* End of FONT loop             */
  539.  
  540. }
  541.  
  542. /*                                                                      */
  543. /*      COLORDEMO: Display the current color palette on the screen.     */
  544. /*                                                                      */
  545.  
  546. void ColorDemo(void)
  547. {
  548.   struct viewporttype vp;
  549.   int color, height, width;
  550.   int x, y, i, j;
  551.   char cnum[5];
  552.  
  553.   MainWindow( "Color Demonstration" );  /* Show demonstration name      */
  554.  
  555.   color = 1;
  556.   getviewsettings( &vp );               /* Get the current window size  */
  557.   width  = 2 * ( (vp.right+1) / 16 );      /* Get box dimensions           */
  558.   height = 2 * ( (vp.bottom-10) / 10 );
  559.  
  560.   x = width / 2;
  561.   y = height / 2;       /* Leave 1/2 box border         */
  562.  
  563.   for( j=0 ; j<3 ; ++j ){               /* Row loop                     */
  564.  
  565.     for( i=0 ; i<5 ; ++i ){             /* Column loop                  */
  566.  
  567.       setfillstyle(SOLID_FILL, color);  /* Set to solid fill in color   */
  568.       setcolor( color );                /* Set the same border color    */
  569.  
  570.       bar( x, y, x+width, y+height );   /* Draw the rectangle           */
  571.       rectangle( x, y, x+width, y+height );  /* outline the rectangle   */
  572.  
  573.       if( color == BLACK ){             /* If box was black...          */
  574.     setcolor( WHITE );              /* Set drawing color to white   */
  575.     rectangle( x, y, x+width, y+height );  /* Outline black in white*/
  576.       }
  577.  
  578.       itoa( color, cnum, 10 );          /* Convert # to ASCII           */
  579.       outtextxy( x+(width/2), y+height+4, cnum );  /* Show color #      */
  580.  
  581.       color = ++color % MaxColors;      /* Advance to the next color    */
  582.       x += (width / 2) * 3;             /* move the column base         */
  583.     }                           /* End of Column loop           */
  584.  
  585.     y += (height / 2) * 3;              /* move the row base            */
  586.     x = width / 2;                      /* reset column base            */
  587.   }                                     /* End of Row loop              */
  588.  
  589.   Pause();                              /* Pause for user's response    */
  590.  
  591. }
  592.  
  593. /*                                                                      */
  594. /*      ARCDEMO: Display a random pattern of arcs on the screen */
  595. /*      until the user says enough.                                     */
  596. /*                                                                      */
  597.  
  598. void ArcDemo(void)
  599. {
  600.   int mradius;                          /* Maximum radius allowed       */
  601.   int eangle;                           /* Random end angle of Arc      */
  602.   struct arccoordstype ai;              /* Used to read Arc Cord info   */
  603.  
  604.   MainWindow( "Arc Demonstration" );
  605.   StatusLine( StopMsg);
  606.  
  607.   mradius = MaxY / 10;                  /* Determine the maximum radius */
  608.  
  609.   while( !kbhit() ){                    /* Repeat until a key is hit    */
  610.     setcolor( random( MaxColors - 1 ) + 1 );    /* Randomly select a color      */
  611.     eangle = random( 358 ) + 1;         /* Select an end angle          */
  612.     arc( random(MaxX), random(MaxY), random(eangle), eangle, mradius );
  613.     getarccoords( &ai );                /* Read Cord data               */
  614.     line( ai.x, ai.y, ai.xstart, ai.ystart ); /* line from start to center */
  615.     line( ai.x, ai.y,   ai.xend,   ai.yend ); /* line from end to center   */
  616.   }                                     /* End of WHILE not KBHIT       */
  617.  
  618.   Pause();                              /* Wait for user's response     */
  619.  
  620. }
  621.  
  622. /*                                                                      */
  623. /*      CIRCLEDEMO: Display a random pattern of circles on the screen   */
  624. /*      until the user says enough.                                     */
  625. /*                                                                      */
  626.  
  627. void CircleDemo(void)
  628. {
  629.   int mradius;                          /* Maximum radius allowed       */
  630.  
  631.   MainWindow( "Circle Demonstration" );
  632.   StatusLine( StopMsg);
  633.  
  634.   mradius = MaxY / 10;                  /* Determine the maximum radius */
  635.  
  636.   while( !kbhit() ){                    /* Repeat until a key is hit    */
  637.     setcolor( random( MaxColors - 1 ) + 1 );    /* Randomly select a color      */
  638.     circle( random(MaxX), random(MaxY), random(mradius) );
  639.   }                                     /* End of WHILE not KBHIT       */
  640.  
  641.   Pause();                              /* Wait for user's response     */
  642.  
  643. }
  644.  
  645. /*                                                                      */
  646. /*      PIEDEMO: Display a pie chart on the screen.                     */
  647. /*                                                                      */
  648.  
  649. #define adjasp( y )     ((int)(AspectRatio * (double)(y)))
  650. #define torad( d )      (( (double)(d) * PI ) / 180.0 )
  651.  
  652. void PieDemo(void)
  653. {
  654.   struct viewporttype vp;
  655.   int xcenter, ycenter, radius, lradius;
  656.   int x, y;
  657.   double radians, piesize;
  658.  
  659.   MainWindow( "Pie Chart Demonstration" );
  660.  
  661.   getviewsettings( &vp );               /* Get the current viewport     */
  662.   xcenter = (vp.right - vp.left) / 2;   /* Center the Pie horizontally  */
  663.   ycenter = (vp.bottom - vp.top) / 2+20;/* Center the Pie vertically    */
  664.   radius  = (vp.bottom - vp.top) / 3;   /* It will cover 2/3rds screen  */
  665.   piesize = (vp.bottom - vp.top) / 4.0; /* Optimum height ratio of pie  */
  666.  
  667.   while( (AspectRatio*radius) < piesize ) ++radius;
  668.  
  669.   lradius = radius + ( radius / 5 );    /* Labels placed 20% farther    */
  670.  
  671.   changetextstyle( TRIPLEX_FONT, HORIZ_DIR, 4 );
  672.   settextjustify( CENTER_TEXT, TOP_TEXT );
  673.   outtextxy( MaxX/2, 6, "This is a Pie Chart" );
  674.   changetextstyle( TRIPLEX_FONT, HORIZ_DIR, 1 );
  675.   settextjustify( CENTER_TEXT, TOP_TEXT );
  676.  
  677.   setfillstyle( SOLID_FILL, RED );
  678.   pieslice( xcenter+10, ycenter-adjasp(10), 0, 90, radius );
  679.   radians = torad( 45 );
  680.   x = xcenter + (int)( cos( radians ) * (double)lradius );
  681.   y = ycenter - (int)( sin( radians ) * (double)lradius * AspectRatio );
  682.   settextjustify( LEFT_TEXT, BOTTOM_TEXT );
  683.   outtextxy( x, y, "25 %" );
  684.  
  685.   setfillstyle( WIDE_DOT_FILL, GREEN );
  686.   pieslice( xcenter, ycenter, 90, 135, radius );
  687.   radians = torad( 113 );
  688.   x = xcenter + (int)( cos( radians ) * (double)lradius );
  689.   y = ycenter - (int)( sin( radians ) * (double)lradius * AspectRatio );
  690.   settextjustify( RIGHT_TEXT, BOTTOM_TEXT );
  691.   outtextxy( x, y, "12.5 %" );
  692.  
  693.   setfillstyle( INTERLEAVE_FILL, YELLOW );
  694.   settextjustify( RIGHT_TEXT, CENTER_TEXT );
  695.   pieslice( xcenter-10, ycenter, 135, 225, radius );
  696.   radians = torad( 180 );
  697.   x = xcenter + (int)( cos( radians ) * (double)lradius );
  698.   y = ycenter - (int)( sin( radians ) * (double)lradius * AspectRatio );
  699.   settextjustify( RIGHT_TEXT, CENTER_TEXT );
  700.   outtextxy( x, y, "25 %" );
  701.  
  702.   setfillstyle( HATCH_FILL, BLUE );
  703.   pieslice( xcenter, ycenter, 225, 360, radius );
  704.   radians = torad( 293 );
  705.   x = xcenter + (int)( cos( radians ) * (double)lradius );
  706.   y = ycenter - (int)( sin( radians ) * (double)lradius * AspectRatio );
  707.   settextjustify( LEFT_TEXT, TOP_TEXT );
  708.   outtextxy( x, y, "37.5 %" );
  709.  
  710.   Pause();                              /* Pause for user's response    */
  711.  
  712. }
  713.  
  714. /*                                                                      */
  715. /*      BARDEMO: Draw a 2-D bar chart using Bar and Rectangle.          */
  716. /*                                                                      */
  717.  
  718. void BarDemo(void)
  719. {
  720.   int barheight[] = {
  721.     1, 3, 5, 2, 4   };
  722.   int styles[]    = {
  723.     1, 3, 10, 5, 9, 1   };
  724.   int xstep, ystep;
  725.   int sheight, swidth;
  726.   int i, j, h;
  727.   struct viewporttype vp;
  728.   char buffer[40];
  729.  
  730.   MainWindow( "Bar / Rectangle demonstration" );
  731.   h = 3 * textheight( "H" );
  732.   getviewsettings( &vp );
  733.   settextjustify( CENTER_TEXT, TOP_TEXT );
  734.   changetextstyle( TRIPLEX_FONT, HORIZ_DIR, 4 );
  735.   outtextxy( MaxX /2, 6, "These are 2-D Bars" );
  736.   changetextstyle( DEFAULT_FONT, HORIZ_DIR, 1 );
  737.   setviewport( vp.left+50, vp.top+30, vp.right-50, vp.bottom-10, 1 );
  738.  
  739.   getviewsettings( &vp );
  740.   sheight = vp.bottom - vp.top;
  741.   swidth  = vp.right  - vp.left;
  742.  
  743.   line( h, h, h, sheight-h );
  744.   line( h, sheight-h, sheight-h, sheight-h );
  745.   ystep = (sheight - (2*h) ) / 5;
  746.   xstep = (swidth  - (2*h) ) / 5;
  747.   j = sheight - h;
  748.   settextjustify( LEFT_TEXT, CENTER_TEXT );
  749.  
  750.   for( i=0 ; i<6 ; ++i ){
  751.     line( h/2, j, h, j );
  752.     itoa( i, buffer, 10 );
  753.     outtextxy( 0, j, buffer );
  754.     j -= ystep;
  755.   }
  756.  
  757.   j = h;
  758.   settextjustify( CENTER_TEXT, TOP_TEXT );
  759.   for( i=0 ; i<6 ; ++i ){
  760.     setfillstyle( styles[i], random(MaxColors) );
  761.     line( j, sheight - h, j, sheight- 3 - (h/2) );
  762.     itoa( i, buffer, 10 );
  763.     outtextxy( j, sheight - (h/2), buffer );
  764.     if( i != 5 ){
  765.       bar( j, (sheight-h)-(barheight[i] * ystep), j+xstep, sheight-h-1 );
  766.       rectangle( j, (sheight-h)-(barheight[i] * ystep), j+xstep, sheight-h);
  767.     }
  768.     j += xstep;
  769.   }
  770.  
  771.   Pause();
  772.  
  773. }
  774.  
  775. /*                                                                      */
  776. /*      LINERELDEMO: Display pattern using moverel and linerel cmds.    */
  777. /*                                                                      */
  778.  
  779. void LineRelDemo(void)
  780. {
  781.   struct viewporttype vp;
  782.   int h, w, dx, dy, cx, cy;
  783.   struct PTS outs[7];
  784.  
  785.  
  786.   MainWindow( "MoveRel / LineRel Demonstration" );
  787.   StatusLine( StopMsg);
  788.  
  789.   getviewsettings( &vp );
  790.   cx = (vp.right  - vp.left) / 2;       /* Center of the screen coords  */
  791.   cy = (vp.bottom - vp.top ) / 2;
  792.  
  793.   h  = (vp.bottom - vp.top ) / 8;
  794.   w  = (vp.right  - vp.left) / 9;
  795.  
  796.   dx = 2 * w;
  797.   dy = 2 * h;
  798.  
  799.   setcolor( BLACK );
  800.  
  801.   setfillstyle( SOLID_FILL, BLUE );
  802.   bar( 0, 0, vp.right-vp.left, vp.bottom-vp.top );      /* Draw backgnd */
  803.  
  804.   outs[0].x = cx -  dx;
  805.   outs[0].y = cy -  dy;
  806.   outs[1].x = cx - (dx-w);
  807.   outs[1].y = cy - (dy+h);
  808.   outs[2].x = cx +  dx;
  809.   outs[2].y = cy - (dy+h);
  810.   outs[3].x = cx +  dx;
  811.   outs[3].y = cy +  dy;
  812.   outs[4].x = cx + (dx-w);
  813.   outs[4].y = cy + (dy+h);
  814.   outs[5].x = cx -  dx;
  815.   outs[5].y = cy + (dy+h);
  816.   outs[6].x = cx -  dx;
  817.   outs[6].y = cy -  dy;
  818.  
  819.   setfillstyle( SOLID_FILL, WHITE );
  820.   fillpoly( 7, (int far *)outs );
  821.  
  822.   outs[0].x = cx - (w/2);
  823.   outs[0].y = cy + h;
  824.   outs[1].x = cx + (w/2);
  825.   outs[1].y = cy + h;
  826.   outs[2].x = cx + (w/2);
  827.   outs[2].y = cy - h;
  828.   outs[3].x = cx - (w/2);
  829.   outs[3].y = cy - h;
  830.   outs[4].x = cx - (w/2);
  831.   outs[4].y = cy + h;
  832.  
  833.   setfillstyle( SOLID_FILL, BLUE );
  834.   fillpoly( 5, (int far *)outs );
  835.  
  836.   /*    Draw a Tesseract object on the screen using the LineRel and     */
  837.   /*    MoveRel drawing commands.                                       */
  838.  
  839.   moveto( cx-dx, cy-dy );
  840.   linerel(  w, -h );
  841.   linerel(  3*w,        0 );
  842.   linerel(   0,  5*h );
  843.   linerel( -w,  h );
  844.   linerel( -3*w,        0 );
  845.   linerel(   0, -5*h );
  846.  
  847.   moverel( w, -h );
  848.   linerel(   0,  5*h );
  849.   linerel( w+(w/2), 0 );
  850.   linerel(   0, -3*h );
  851.   linerel( w/2,   -h );
  852.   linerel( 0, 5*h );
  853.  
  854.   moverel(  0, -5*h );
  855.   linerel( -(w+(w/2)), 0 );
  856.   linerel( 0, 3*h );
  857.   linerel( -w/2, h );
  858.  
  859.   moverel( w/2, -h );
  860.   linerel( w, 0 );
  861.  
  862.   moverel( 0, -2*h );
  863.   linerel( -w, 0 );
  864.  
  865.   Pause();                              /* Wait for user's response     */
  866.  
  867. }
  868.  
  869. /*                                                                      */
  870. /*      PUTPIXELDEMO: Display a pattern of random dots on the screen    */
  871. /*      and pick them back up again.                                    */
  872. /*                                                                      */
  873.  
  874. void PutPixelDemo(void)
  875. {
  876.   int seed = 1958;
  877.   int i, x, y, h, w, color;
  878.   struct viewporttype vp;
  879.  
  880.   MainWindow( "PutPixel / GetPixel Demonstration" );
  881.   StatusLine( PauseMsg);                /* Put msg at bottom of screen   */
  882.  
  883.   getviewsettings( &vp );
  884.   h = vp.bottom - vp.top;
  885.   w = vp.right  - vp.left;
  886.  
  887.   do {
  888.     srand( seed );                      /* Restart random # function    */
  889.  
  890.     for( i=0 ; i<5000 ; ++i ){          /* Put 5000 pixels on screen    */
  891.       x = 1 + random( w - 1 );          /* Generate a random location   */
  892.       y = 1 + random( h - 1 );
  893.       color = random( MaxColors );
  894.       putpixel( x, y, color );
  895.     }
  896.  
  897.     srand( seed );                      /* Restart Random # at same #   */
  898.  
  899.     for( i=0 ; i<5000 ; ++i ){          /* Take the 5000 pixels off     */
  900.       x = 1 + random( w - 1 );          /* Generate a random location   */
  901.       y = 1 + random( h - 1 );
  902.       color = getpixel( x, y );         /* Read the color pixel         */
  903.       if( color == random( MaxColors ) )        /* Used to keep RANDOM in sync  */
  904.     putpixel( x, y, 0 );            /* Write pixel to BLACK         */
  905.     }
  906.     if (!kbhit())
  907.       delay(400);
  908.   } while ( !kbhit());
  909.  
  910.   Pause();                              /* Wait for user's response     */
  911.  
  912. }
  913.  
  914. /*                                                                      */
  915. /*   PUTIMAGEDEMO                                                       */
  916. /*                                                                      */
  917. #define PAUSETIME 20
  918. #define PID_r     20
  919. #define StartX    100
  920. #define StartY    50
  921. #define MAXXSTEP  (2*PID_r/3)
  922. #define MAXYSTEP  (PID_r/2)
  923. #define PID_STEPS 250
  924.  
  925. void PutImageDemo(void)
  926. {
  927.   struct viewporttype vp;
  928.   int    x, y, ulx, uly, lrx, lry, size, i, width, height;
  929.   int    nx, ny, dx, dy;
  930.   void   *Saucer;
  931.  
  932.   MainWindow("GetImage / PutImage Demonstration");
  933.   getviewsettings( &vp );
  934.  
  935.   /* DrawSaucer */
  936.   ellipse(StartX, StartY, 0, 360, PID_r, PID_r / 3 + 2);
  937.   ellipse(StartX, StartY - 4, 190, 357, PID_r, PID_r / 3);
  938.   line(StartX + 7, StartY - 6, StartX + 10, StartY - 12);
  939.   circle(StartX + 10, StartY - 12, 2);
  940.   line(StartX - 7, StartY - 6, StartX - 10, StartY - 12);
  941.   circle(StartX - 10, StartY - 12, 2);
  942.   setfillstyle(SOLID_FILL, WHITE);
  943.   floodfill(StartX + 1, StartY + 4, getcolor());
  944.  
  945.   /* Read saucer image */
  946.   ulx = StartX-(PID_r+1);
  947.   uly = StartY-14;
  948.   lrx = StartX+(PID_r+1);
  949.   lry = StartY+(PID_r/3)+3;
  950.   width = lrx - ulx + 1;
  951.   height = lry - uly + 1;
  952.   size = imagesize(ulx, uly, lrx, lry);
  953.  
  954.   Saucer = malloc( size );
  955.   if (Saucer == NULL) return;
  956.   getimage(ulx, uly, lrx, lry, Saucer);
  957.   putimage(ulx, uly, Saucer, XOR_PUT);
  958.  
  959.   /* Plot some "stars"  */
  960.   for ( i=0 ; i<1000; ++i )
  961.     putpixel(random(MaxX), random(MaxY), random( MaxColors-1 )+1);
  962.   x = MaxX / 2;
  963.   y = MaxY / 2;
  964.   dx = 1;
  965.   dy = 0;
  966.  
  967.   StatusLine( PauseMsg);                /* Put msg at bottom of screen   */
  968.  
  969.   /* until a key is hit */
  970.   while ( !kbhit() ) {
  971.  
  972.     /* Draw the Saucer */
  973.     if (dx != 0 || dy != 0)
  974.       putimage(x, y, Saucer, XOR_PUT);               /*  draw image  */
  975.     delay(PAUSETIME);
  976.     dx += (random( 2*MAXXSTEP+1) - MAXXSTEP + MAXXSTEP*(MaxX/2-x)/MaxX) / 10;
  977.     if ( dx >  MAXXSTEP) dx =  MAXXSTEP;
  978.     if ( dx < -MAXXSTEP) dx = -MAXXSTEP;
  979.     nx = x + dx;
  980.     if (vp.left + nx + width - 1 > vp.right)
  981.       nx = vp.right-vp.left-width + 1;
  982.     else
  983.       if (nx < 0)
  984.     nx = 0;
  985.     dy += (random( 2*MAXYSTEP+1) - MAXYSTEP + MAXYSTEP*(MaxY/2-y)/MaxY) / 10;
  986.     if ( dy >  MAXXSTEP) dy =  MAXXSTEP;
  987.     if ( dy < -MAXXSTEP) dy = -MAXXSTEP;
  988.     ny = y + dy;
  989.     if (vp.top + ny + height - 1 > vp.bottom)
  990.       ny = vp.bottom-vp.top-height + 1;
  991.     else
  992.       if (ny < 0)
  993.     ny = 0;
  994.     dx = nx-x;
  995.     dy = ny-y;
  996.     if (dx != 0 || dy != 0)
  997.       putimage(x, y, Saucer, XOR_PUT);               /* erase image  */
  998.     x = nx;
  999.     y = ny;
  1000.   }
  1001.   free( Saucer );
  1002.   Pause();
  1003. }
  1004.  
  1005.  
  1006. /*                                                                      */
  1007. /*      LINETODEMO: Display a pattern using moveto and lineto commands. */
  1008. /*                                                                      */
  1009.  
  1010. #define MAXPTS  15
  1011.  
  1012. void LineToDemo(void)
  1013. {
  1014.   struct viewporttype vp;
  1015.   struct PTS points[MAXPTS];
  1016.   int i, j, h, w, xcenter, ycenter;
  1017.   int radius, angle, step;
  1018.   double  rads;
  1019.  
  1020.   MainWindow( "MoveTo / LineTo Demonstration" );
  1021.  
  1022.   getviewsettings( &vp );
  1023.   h = vp.bottom - vp.top;
  1024.   w = vp.right  - vp.left;
  1025.  
  1026.   xcenter = w / 2;                      /* Determine the center of circle */
  1027.   ycenter = h / 2;
  1028.   radius  = (h - 30) / (AspectRatio * 2);
  1029.   step    = 360 / MAXPTS;               /* Determine # of increments    */
  1030.  
  1031.   angle = 0;                            /* Begin at zero degrees        */
  1032.   for( i=0 ; i<MAXPTS ; ++i ){          /* Determine circle intercepts  */
  1033.     rads = (double)angle * PI / 180.0;  /* Convert angle to radians     */
  1034.     points[i].x = xcenter + (int)( cos(rads) * radius );
  1035.     points[i].y = ycenter - (int)( sin(rads) * radius * AspectRatio );
  1036.     angle += step;                      /* Move to next increment       */
  1037.   }
  1038.  
  1039.   circle( xcenter, ycenter, radius );   /* Draw bounding circle         */
  1040.  
  1041.   for( i=0 ; i<MAXPTS ; ++i ){          /* Draw the cords to the circle */
  1042.     for( j=i ; j<MAXPTS ; ++j ){        /* For each remaining intersect */
  1043.       moveto(points[i].x, points[i].y); /* Move to beginning of cord    */
  1044.       lineto(points[j].x, points[j].y); /* Draw the cord                */
  1045.     }
  1046.   }
  1047.  
  1048.   Pause();                              /* Wait for user's response     */
  1049.  
  1050. }
  1051.  
  1052. /*                                                                      */
  1053. /*      LINESTYLEDEMO: Display a pattern using all of the standard      */
  1054. /*      line styles that are available.                                 */
  1055. /*                                                                      */
  1056.  
  1057. void LineStyleDemo(void)
  1058. {
  1059.   int style, step;
  1060.   int x, y, w;
  1061.   struct viewporttype vp;
  1062.   char buffer[40];
  1063.  
  1064.   MainWindow( "Pre-defined line styles" );
  1065.  
  1066.   getviewsettings( &vp );
  1067.   w = vp.right  - vp.left;
  1068.  
  1069.   x = 35;
  1070.   y = 10;
  1071.   step = w / 11;
  1072.  
  1073.   settextjustify( LEFT_TEXT, TOP_TEXT );
  1074.   outtextxy( x, y, "Normal Width" );
  1075.  
  1076.   settextjustify( CENTER_TEXT, TOP_TEXT );
  1077.  
  1078.   for( style=0 ; style<4 ; ++style ){
  1079.     setlinestyle( style, 0, NORM_WIDTH );
  1080.     line( x, y+20, x, vp.bottom-40 );
  1081.     itoa( style, buffer, 10 );
  1082.     outtextxy( x, vp.bottom-30, buffer );
  1083.     x += step;
  1084.   }
  1085.  
  1086.   x += 2 * step;
  1087.  
  1088.   settextjustify( LEFT_TEXT, TOP_TEXT );
  1089.   outtextxy( x, y, "Thick Width" );
  1090.   settextjustify( CENTER_TEXT, TOP_TEXT );
  1091.  
  1092.   for( style=0 ; style<4 ; ++style ){
  1093.     setlinestyle( style, 0, THICK_WIDTH );
  1094.     line( x, y+20, x, vp.bottom-40 );
  1095.     itoa( style, buffer, 10 );
  1096.     outtextxy( x, vp.bottom-30, buffer );
  1097.     x += step;
  1098.   }
  1099.  
  1100.   settextjustify( LEFT_TEXT, TOP_TEXT );
  1101.  
  1102.   Pause();                              /* Wait for user's response     */
  1103.  
  1104. }
  1105.  
  1106. /*                                                                      */
  1107. /*      CRTMODEDEMO: Demonstrate the effects of the change mode         */
  1108. /*      commands on the current screen.                                 */
  1109. /*                                                                      */
  1110.  
  1111. void CRTModeDemo(void)
  1112. {
  1113.   struct viewporttype vp;
  1114.   int mode;
  1115.  
  1116.   MainWindow( "SetGraphMode / RestoreCRTMode demo" );
  1117.   getviewsettings( &vp );
  1118.   mode = getgraphmode();
  1119.   settextjustify( CENTER_TEXT, CENTER_TEXT );
  1120.  
  1121.   outtextxy( (vp.right-vp.left)/2, (vp.bottom-vp.top)/2,
  1122.   "Now you are in graphics mode..." );
  1123.   StatusLine( "Press any key for text mode..." );
  1124.   getch();
  1125.  
  1126.   restorecrtmode();
  1127.   printf( "Now you are in text mode.\n\n" );
  1128.   printf( "Press any key to go back to graphics..." );
  1129.   fflush(stdout);
  1130.   getch();
  1131.  
  1132.   setgraphmode( mode );
  1133.   MainWindow( "SetGraphMode / RestoreCRTMode demo" );
  1134.   settextjustify( CENTER_TEXT, CENTER_TEXT );
  1135.   outtextxy( (vp.right-vp.left)/2, (vp.bottom-vp.top)/2,
  1136.   "Back in Graphics Mode..." );
  1137.  
  1138.   Pause();                              /* Wait for user's response     */
  1139.  
  1140. }
  1141.  
  1142. /*                                                                      */
  1143. /*      USERLINESTYLEDEMO: Display line styles showing the user         */
  1144. /*      defined line style functions.                                   */
  1145. /*                                                                      */
  1146.  
  1147. void UserLineStyleDemo(void)
  1148. {
  1149.   int x, y, i, h, flag;
  1150.   unsigned int style;
  1151.   struct viewporttype vp;
  1152.  
  1153.   MainWindow( "User defined line styles" );
  1154.  
  1155.   getviewsettings( &vp );
  1156.   h = vp.bottom - vp.top;
  1157.  
  1158.   x = 4;
  1159.   y = 10;
  1160.   style = 0;
  1161.   i = 0;
  1162.  
  1163.   settextjustify( CENTER_TEXT, TOP_TEXT );
  1164.   flag = TRUE;                          /* Set the bits in this pass    */
  1165.  
  1166.   while( x < vp.right-2 ){              /* Draw lines across the screen */
  1167.  
  1168.     if( flag )                          /* If flag, set bits...         */
  1169.       style |= (1 << i);                /*    Set the Ith bit in word   */
  1170.     else                                /* If no flag, clear bits       */
  1171.       style &= ~(1<<i);                 /*    Clear the Ith bit in word */
  1172.  
  1173.     setlinestyle( USERBIT_LINE, style, NORM_WIDTH );
  1174.     line( x, y, x, h-y );               /* Draw the new line pattern    */
  1175.  
  1176.     x += 5;                             /* Move the X location of line  */
  1177.     i = ++i % 16;                       /* Advance to next bit pattern  */
  1178.  
  1179.     if( style == 0xffff ){              /* Are all bits set?            */
  1180.       flag = FALSE;                     /*   begin removing bits        */
  1181.       i = 0;                            /* Start with whole pattern     */
  1182.     }
  1183.     else {                              /* Bits not all set...          */
  1184.       if( style == 0 ) {                /* Are all bits clear?          */
  1185.     flag = TRUE;                    /*   begin setting bits         */
  1186.     i = 0;
  1187.       }
  1188.     }
  1189.   }
  1190.  
  1191.   settextjustify( LEFT_TEXT, TOP_TEXT );
  1192.  
  1193.   Pause();                              /* Wait for user's response     */
  1194.  
  1195. }
  1196.  
  1197. /*                                                                      */
  1198. /*      FILLSTYLEDEMO: Display the standard fill patterns available.    */
  1199. /*                                                                      */
  1200.  
  1201. void FillStyleDemo(void)
  1202. {
  1203.   int h, w, style;
  1204.   int i, j, x, y;
  1205.   struct viewporttype vp;
  1206.   char buffer[40];
  1207.  
  1208.   MainWindow( "Pre-defined Fill Styles" );
  1209.  
  1210.   getviewsettings( &vp );
  1211.   w = 2 * ((vp.right  +  1) / 13);
  1212.   h = 2 * ((vp.bottom - 10) / 10);
  1213.  
  1214.   x = w / 2;
  1215.   y = h / 2;            /* Leave 1/2 blk margin         */
  1216.   style = 0;
  1217.  
  1218.   for( j=0 ; j<3 ; ++j ){               /* Three rows of boxes          */
  1219.     for( i=0 ; i<4 ; ++i ){             /* Four column of boxes         */
  1220.       setfillstyle(style, WHITE);       /* Set the fill style and WHITE */
  1221.       bar( x, y, x+w, y+h );            /* Draw the actual box          */
  1222.       rectangle( x, y, x+w, y+h );      /* Outline the box              */
  1223.       itoa( style, buffer, 10 );        /* Convert style 3 to ASCII     */
  1224.       outtextxy( x+(w / 2), y+h+4, buffer );
  1225.       ++style;                          /* Go on to next style #        */
  1226.       x += (w / 2) * 3;                 /* Go to next column            */
  1227.     }                           /* End of coulmn loop           */
  1228.     x = w / 2;                          /* Put base back to 1st column  */
  1229.     y += (h / 2) * 3;                   /* Advance to next row          */
  1230.   }                                     /* End of Row loop              */
  1231.  
  1232.   settextjustify( LEFT_TEXT, TOP_TEXT );
  1233.  
  1234.   Pause();                              /* Wait for user's response     */
  1235.  
  1236. }
  1237.  
  1238. /*                                                                      */
  1239. /*      FILLPATTERNDEMO: Demonstrate how to use the user definable      */
  1240. /*      fill patterns.                                                  */
  1241. /*                                                                      */
  1242.  
  1243. void FillPatternDemo(void)
  1244. {
  1245.   int style;
  1246.   int h, w;
  1247.   int x, y, i, j;
  1248.   char buffer[40];
  1249.   struct viewporttype vp;
  1250.   static char patterns[][8] = {
  1251.     { 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55 },
  1252.     { 0x33, 0x33, 0xCC, 0xCC, 0x33, 0x33, 0xCC, 0xCC },
  1253.     { 0xF0, 0xF0, 0xF0, 0xF0, 0x0F, 0x0F, 0x0F, 0x0F },
  1254.     { 0x00, 0x10, 0x28, 0x44, 0x28, 0x10, 0x00, 0x00 },
  1255.     { 0x00, 0x70, 0x20, 0x27, 0x24, 0x24, 0x07, 0x00 },
  1256.     { 0x00, 0x00, 0x00, 0x18, 0x18, 0x00, 0x00, 0x00 },
  1257.     { 0x00, 0x00, 0x3C, 0x3C, 0x3C, 0x3C, 0x00, 0x00 },
  1258.     { 0x00, 0x7E, 0x7E, 0x7E, 0x7E, 0x7E, 0x7E, 0x00 },
  1259.     { 0x00, 0x00, 0x22, 0x08, 0x00, 0x22, 0x1C, 0x00 },
  1260.     { 0xFF, 0x7E, 0x3C, 0x18, 0x18, 0x3C, 0x7E, 0xFF },
  1261.     { 0x00, 0x10, 0x10, 0x7C, 0x10, 0x10, 0x00, 0x00 },
  1262.     { 0x00, 0x42, 0x24, 0x18, 0x18, 0x24, 0x42, 0x00 }
  1263.   };
  1264.  
  1265.   MainWindow( "User Defined Fill Styles" );
  1266.  
  1267.   getviewsettings( &vp );
  1268.   w = 2 * ((vp.right  +  1) / 13);
  1269.   h = 2 * ((vp.bottom - 10) / 10);
  1270.  
  1271.   x = w / 2;
  1272.   y = h / 2;            /* Leave 1/2 blk margin         */
  1273.   style = 0;
  1274.  
  1275.   for( j=0 ; j<3 ; ++j ){               /* Three rows of boxes          */
  1276.     for( i=0 ; i<4 ; ++i ){             /* Four column of boxes         */
  1277.       setfillpattern( &patterns[style][0], WHITE);
  1278.       bar( x, y, x+w, y+h );            /* Draw the actual box          */
  1279.       rectangle( x, y, x+w, y+h );      /* Outline the box              */
  1280.       itoa( style, buffer, 10 );        /* Convert style 3 to ASCII     */
  1281.       outtextxy( x+(w / 2), y+h+4, buffer );
  1282.       ++style;                          /* Go on to next style #        */
  1283.       x += (w / 2) * 3;                 /* Go to next column            */
  1284.     }                           /* End of coulmn loop           */
  1285.     x = w / 2;                          /* Put base back to 1st column  */
  1286.     y += (h / 2) * 3;                   /* Advance to next row          */
  1287.   }                                     /* End of Row loop              */
  1288.  
  1289.   settextjustify( LEFT_TEXT, TOP_TEXT );
  1290.  
  1291.   Pause();                              /* Wait for user's response     */
  1292.  
  1293. }
  1294.  
  1295. /*                                                                      */
  1296. /*      POLYDEMO: Display a random pattern of polygons on the screen    */
  1297. /*      until the user says enough.                                     */
  1298. /*                                                                      */
  1299. void PaletteDemo(void)
  1300. {
  1301.   int i, j, x, y, color, cols;
  1302.   struct viewporttype vp;
  1303.   int height, width;
  1304.  
  1305.   if (MaxColors > 16)
  1306.     return;
  1307.   MainWindow( "Palette Demonstration" );
  1308.   StatusLine( StopMsg);
  1309.  
  1310.   getviewsettings( &vp );
  1311.   width  = (vp.right - vp.left) / 15;   /* get width of the box         */
  1312.   height = (vp.bottom - vp.top) / 10;   /* Get the height of the box    */
  1313.  
  1314.   x = y = 0;                            /* Start in upper corner        */
  1315.   color = 1;                            /* Begin at 1st color           */
  1316.   cols = 16;
  1317.   if (MaxColors < cols) cols = MaxColors;
  1318.  
  1319.   for( j=0 ; j<10 ; ++j ){              /* For 10 rows of boxes         */
  1320.     for( i=0 ; i<15 ; ++i ){            /* For 15 columns of boxes      */
  1321.       setfillstyle( SOLID_FILL, color++ );      /* Set the color of box */
  1322.       bar( x, y, x+width, y+height );           /* Draw the box         */
  1323.       x += width + 1;                           /* Advance to next col  */
  1324.       color = 1 + (color % (cols- 2));          /* Set new color        */
  1325.     }                                   /* End of COLUMN loop           */
  1326.     x = 0;                              /* Goto 1st column              */
  1327.     y += height + 1;                    /* Goto next row                */
  1328.   }                                     /* End of ROW loop              */
  1329.  
  1330.   while( !kbhit() ){                    /* Until user enters a key...   */
  1331.     setpalette( 1+random(cols - 2), random(64) );
  1332.   }
  1333.  
  1334.   setallpalette( &palette );
  1335.  
  1336.   Pause();                              /* Wait for user's response     */
  1337.  
  1338. }
  1339.  
  1340. /*                                                                      */
  1341. /*      POLYDEMO: Display a random pattern of polygons on the screen    */
  1342. /*      until the user says enough.                                     */
  1343. /*                                                                      */
  1344.  
  1345. #define MaxPts          6               /* Maximum # of pts in polygon  */
  1346.  
  1347. void PolyDemo(void)
  1348. {
  1349.   struct PTS poly[ MaxPts ];            /* Space to hold datapoints     */
  1350.   int color;                            /* Current drawing color        */
  1351.   int i;
  1352.  
  1353.   MainWindow( "DrawPoly / FillPoly Demonstration" );
  1354.   StatusLine( StopMsg);
  1355.  
  1356.   while( !kbhit() ){                    /* Repeat until a key is hit    */
  1357.  
  1358.     color = 1 + random( MaxColors-1 );  /* Get a random color # (no blk)*/
  1359.     setfillstyle( random(10), color );  /* Set a random line style      */
  1360.     setcolor( color );                  /* Set the desired color        */
  1361.  
  1362.     for( i=0 ; i<(MaxPts-1) ; i++ ){    /* Determine a random polygon   */
  1363.       poly[i].x = random( MaxX );       /* Set the x coord of point     */
  1364.       poly[i].y = random( MaxY );       /* Set the y coord of point     */
  1365.     }
  1366.  
  1367.     poly[i].x = poly[0].x;              /* last point = first point     */
  1368.     poly[i].y = poly[1].y;
  1369.  
  1370.     fillpoly( MaxPts, (int far *)poly );    /* Draw the actual polygon      */
  1371.   }                                     /* End of WHILE not KBHIT       */
  1372.  
  1373.   Pause();                              /* Wait for user's response     */
  1374.  
  1375. }
  1376.  
  1377.  
  1378. /*                                                                      */
  1379. /*      SAYGOODBYE: Give a closing screen to the user before leaving.   */
  1380. /*                                                                      */
  1381.  
  1382. void SayGoodbye(void)
  1383. {
  1384.   struct viewporttype viewinfo;         /* Structure to read viewport   */
  1385.   int h, w;
  1386.  
  1387.   MainWindow( "== Finale ==" );
  1388.  
  1389.   getviewsettings( &viewinfo );         /* Read viewport settings       */
  1390.   changetextstyle( TRIPLEX_FONT, HORIZ_DIR, 4 );
  1391.   settextjustify( CENTER_TEXT, CENTER_TEXT );
  1392.  
  1393.   h = viewinfo.bottom - viewinfo.top;
  1394.   w = viewinfo.right  - viewinfo.left;
  1395.   outtextxy( w/2, h/2, "That's all, folks!" );
  1396.  
  1397.   StatusLine( "Press any key to EXIT" );
  1398.   getch();
  1399.  
  1400.   cleardevice();                        /* Clear the graphics screen    */
  1401.  
  1402. }
  1403.  
  1404. /*                                                                      */
  1405. /*      Include demo extensions                                         */
  1406. /*                                                                      */
  1407.  
  1408. #include "htmext.inc"
  1409.  
  1410. /*                                                                      */
  1411. /*      Begin main function                                             */
  1412. /*                                                                      */
  1413.  
  1414. int main()
  1415. {
  1416.  
  1417. #if defined(__GNUC__)
  1418.   registerbgifont( &_bold_font);
  1419.   registerbgifont( &_euro_font);
  1420.   registerbgifont( &_goth_font);
  1421.   registerbgifont( &_lcom_font);
  1422.   registerbgifont( &_litt_font);
  1423.   registerbgifont( &_sans_font);
  1424.   registerbgifont( &_scri_font);
  1425.   registerbgifont( &_simp_font);
  1426.   registerbgifont( &_trip_font);
  1427.   registerbgifont( &_tscr_font);
  1428. #endif
  1429.   Initialize();                 /* Set system into Graphics mode        */
  1430.   ReportStatus();               /* Report results of the initialization */
  1431.  
  1432.   ColorDemo();                  /* Begin actual demonstration           */
  1433.   if( GraphDriver==EGA || GraphDriver==EGA64 || GraphDriver==VGA )
  1434.     PaletteDemo();
  1435.   PutPixelDemo();
  1436.   PutImageDemo();
  1437.   Bar3DDemo();
  1438.   BarDemo();
  1439.   RandomBars();
  1440.   RandomSolidBars();
  1441.   sierpinski();
  1442.   ArcDemo();
  1443.   CircleDemo();
  1444.   PieDemo();
  1445.   PlayRGBpalette();
  1446.   LineRelDemo();
  1447.   LineToDemo();
  1448.   LineStyleDemo();
  1449.   UserLineStyleDemo();
  1450.   TextDump();
  1451.   TextDemo();
  1452.   CRTModeDemo();
  1453.   FillStyleDemo();
  1454.   FillPatternDemo();
  1455.   PolyDemo();
  1456.   SayGoodbye();                 /* Give user the closing screen         */
  1457.   closegraph();                 /* Return the system to text mode       */
  1458.   return(0);
  1459. }
  1460.  
  1461.